Page({
  tips: [{
    score: [0, 1, 2, 3, 4],
    tips: '同志的眼睛是雪白的~'
  }, {
    score: [5, 6, 7, 8],
    tips: '同志的眼神有待提高~'
  }, {
    score: [9, 10, 11, 12],
    tips: '同志的眼神还不赖嘛~'
  }, {
    score: [13, 14, 15, 16],
    tips: '同志的眼神清澈明亮~'
  }, {
    score: [17, 18, 19, 20],
    tips: '同志的眼睛已达极品~'
  }, {
    score: [21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
    tips: '同志的眼睛乃天造地设~'
  }],
  data: {
    block: {},
    randNum: "", // 随机数
    score: 0, // 得分
    deductPoints: 0, // 扣分
    time: 30, // 倒计时
    showScrollPanel: false, // 得分弹框
    topTotalHeight: getApp().globalData.topTotalHeight, // 顶部tabbar总高度
    showScrollPanel2: true, // 开始按钮显示

  },
  start() {
    this.setData({
      showScrollPanel2: false
    })
    this.createBlock(0);
    this.stop();
  },
  again: function () {
    this.setData({
      block: {},
      randNum: "", // 随机数
      score: 0, // 得分
      deductPoints: 0, // 扣分
      time: 30, // 倒计时
      showScrollPanel: false, // 得分弹框
      showScrollPanel2: true
    })
    this.onLoad()
  },
  stop: function () {
    let n = setInterval(() => {
      if (this.data.time > 0) {
        var a = this.data.time;
        a -= 1
        this.setData({
          time: a
        });
      } else {
        clearInterval(n)
        // 匹配tips
        this.tips.forEach(item => {
          if (item.score.includes(this.data.score)) {
            this.setData({
              tips: item.tips
            })
          }
        });
        this.setData({
          showScrollPanel: true
        });
      }
    }, 1e3);
  },
  // 创建下一组色块数据
  createBlock: function (t) {
    var n = [2, 3, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9],
      a = t < n.length ? n[t] : 9,
      o = Math.pow(a, 2),
      e = this.createColor(a),
      i = e[1],
      r = e[0];
    r = "rgb(" + (r = r.map(function (t) {
      return t + 15 * Math.max(9 - a, 1);
    })).join(",") + ")";
    var c = new Array(o).fill(i),
      d = Math.floor(Math.random() * o);
    c[d] = r, this.setData({
      block: {
        lv: a,
        color: c
      },
      randNum: d
    });
  },
  createColor: function (t) {
    var n;
    n = 15 * Math.max(9 - t, 1);
    var a = [Math.round(Math.random() * (255 - n)), Math.round(Math.random() * (255 - n)), Math.round(Math.random() * (255 - n))];
    return [a, "rgb(" + a.join(",") + ")"];
  },
  nextBlock: function (e) {
    if (e.currentTarget.dataset.id === this.data.randNum) {
      this.setData({
        score: this.data.score + 1
      })
    } else {
      this.setData({
        deductPoints: this.data.deductPoints + 1
      })
    }
    this.createBlock(this.data.score);
  },
  onShareAppMessage() {
    return {
      title: "色盲测试，来看看你的成绩吧",
      path: "/pages/dailyTools/pages/colorBlindnessTest/index",
      imageUrl: '/images/function_icon/colorBlindness.png',
    }
  },
  onShareTimeline() {
    return {
      title: '色盲测试，来看看你的成绩吧',
      imageUrl: '/images/function_icon/colorBlindness.png',
    }
  },
});